package com.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.mapper.AddressesListMapper;
import com.manage.mapper.AddressesMapper;
import com.manage.pojo.Addresses;
import com.manage.pojo.operation.AddressesList;
import com.manage.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Service
public class AddressesServiceImpl implements AddressesService{

    @Autowired
    private AddressesMapper addressesMapper;
    @Autowired
    private AddressesListMapper addressesListMapper;
    @Autowired
    private Sid aid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryAddressesIsExit(String addressId) {
        Addresses address = new Addresses();
        address.setAddressId(addressId);
        Addresses result = addressesMapper.selectOne(address);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveAddresses(Addresses address) {
        String addressId = aid.nextShort();
        address.setAddressId(addressId);
        addressesMapper.insert(address);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void addressDelete(String addressId) {
        Example addressExample = new Example(Addresses.class);
        Criteria criteria = addressExample.createCriteria();
        criteria.andEqualTo("addressId", addressId);
        addressesMapper.deleteByExample(addressExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void addressUpdate(Addresses address) {
        Example addressExample = new Example(Addresses.class);
        Criteria criteria = addressExample.createCriteria();
        criteria.andEqualTo("addressId", address.getAddressId());
        addressesMapper.updateByExampleSelective(address, addressExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public AddressesList queryAddressesInfoByAddressId(String addressId) {
        AddressesList result = addressesListMapper.queryAddressesByAddressId(addressId);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryAddressesByUserId(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page, pageSize);
        List<AddressesList> list = addressesListMapper.queryAddressesByUserId(userId);
        PageInfo<AddressesList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }
}
